//
//  SingerPageFromPlayViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/30.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SingerPageFromPlayViewController.h"
#import "ListDetailsTableViewCell.h"
#import "MusicPlayViewController.h"

@interface SingerPageFromPlayViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIButton *backButton;
@property (nonatomic, retain)UITableView *songTableView;
@property (nonatomic, retain)NSMutableArray *songArr;

@property (nonatomic, retain)UIScrollView *bgScrollView;

@property (nonatomic, retain)NSString *songNum;

@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, retain)NSString *imageStr;



@end

@implementation SingerPageFromPlayViewController

- (void)dealloc
{
    [_imageStr release];
    [_bgScrollView release];
    [_songNum release];
    [_HUD release];
    [_songArr release];
    [_songTableView release];
    [_backButton release];
    [_singer release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 release];
    
    
    
    CGFloat ab = 70.0 / 667;
    
    self.bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT - 20 - VIEW_HEIGHT * ab)];
    self.bgScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bgScrollView];
    self.bgScrollView.showsVerticalScrollIndicator = NO;
    [_bgScrollView release];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.bgScrollView];
    [self.bgScrollView addSubview:self.HUD];
    self.HUD.labelText = @"Music is on the way...";
    self.HUD.dimBackground = YES;
    [self.HUD show:YES];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.backButton.frame = CGRectMake(20, 40, 30, 30);
    [self.backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    
    if ([self.singer.artist_id isEqualToString:@"25004634"]) {
        self.singer.artist_id = @"79";
        self.singer.ting_uid = @"1071";
    }
    
    NSString *songStr = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.artist.getSongList&format=json&order=2&tinguid=%@&artistid=%@&offset=0&limits=50", self.singer.ting_uid,self.singer.artist_id];

        [NetHandler getDataWithUrl:songStr completion:^(id dict1) {
            [self setSongPageWithData:dict1];
            [self.HUD hide:YES];
        }];
}

- (void)backButtonAction:(UIButton *)button
{
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    UIView *playView = [window viewWithTag:10001];
    playView.hidden = YES;
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)setSongPageWithData:(id)data
{
    self.songNum = [data objectForKey:@"songnums"];
    
    UIImageView *singerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 250)];
    [singerImageView setImageWithURL:[NSURL URLWithString:[self.singer.avatar_big stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    singerImageView.contentMode = UIViewContentModeScaleAspectFill;
    singerImageView.clipsToBounds = YES;
    [self.bgScrollView addSubview:singerImageView];
    [singerImageView release];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, VIEW_WIDTH, 30)];
    nameLabel.text = self.singer.name;
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.font = [UIFont systemFontOfSize:20];
    [self.bgScrollView addSubview:nameLabel];
    [nameLabel release];
    
    UILabel *areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, VIEW_WIDTH, 20)];
    areaLabel.text = [NSString stringWithFormat:@"%@歌手", self.singer.country];
    
    if (self.singer.country == nil) {
        areaLabel.text = nil;
    }
    
    areaLabel.font = [UIFont systemFontOfSize:16];
    areaLabel.textColor = [UIColor whiteColor];
    [self.bgScrollView addSubview:areaLabel];
    [areaLabel release];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 250, VIEW_WIDTH, 50)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.bgScrollView addSubview:view1];
    [view1 release];
    
    UIButton *playAllButton = [UIButton buttonWithType:UIButtonTypeSystem];
    playAllButton.frame = CGRectMake(20, 10, 30, 30);
    [playAllButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    [view1 addSubview:playAllButton];
    
    UILabel *playAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 100, 30)];
    playAllLabel.text = @"全部播放";
    playAllLabel.textColor = [UIColor grayColor];
    [view1 addSubview:playAllLabel];
    [playAllLabel release];
    
    UIView *view11 = [[UIView alloc] initWithFrame:CGRectMake(10, 299, VIEW_WIDTH - 20, 1)];
    view11.backgroundColor = [UIColor blackColor];
    view11.alpha = 0.1;
    [self.bgScrollView addSubview:view11];
    [view11 release];
    
    self.songArr = [NSMutableArray array];
    NSArray *arr = [data objectForKey:@"songlist"];
    for (NSDictionary *dic in arr) {
        Song *song = [[Song alloc] initWithDictionary:dic];
        [self.songArr addObject:song];
        [song release];
    }
    
    self.songTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 300, VIEW_WIDTH - 10, self.songArr.count * 60) style:UITableViewStylePlain];
    self.songTableView.dataSource = self;
    self.songTableView.delegate = self;
    self.songTableView.rowHeight = 60;
    [self.songTableView registerClass:[ListDetailsTableViewCell class] forCellReuseIdentifier:@"singerSongReuse"];
    [self.bgScrollView addSubview:self.songTableView];
    [_songTableView release];
    
    self.bgScrollView.contentSize = CGSizeMake(0, self.songTableView.frame.size.height + 300);
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"singerSongReuse" forIndexPath:indexPath];
    cell.song = [self.songArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    MusicPlayViewController *musicPlayVC = [MusicPlayViewController shareInstance];
    Song *song = [self.songArr objectAtIndex:indexPath.row];
    musicPlayVC.address = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.play&format=json&callback=&songid=%@&_=1413017198449", song.song_id];
    musicPlayVC.playingNumber = indexPath.row;
    musicPlayVC.playSongArr = [NSMutableArray array];
    for (Song *song in self.songArr) {
        NSString *songID = song.song_id;
        [musicPlayVC.playSongArr addObject:songID];
    }
    musicPlayVC.type = @"music";
    musicPlayVC.songID = song.song_id;
    
//    [self dismissViewControllerAnimated:YES completion:^{
//    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
